package de.baumann.browser.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Objects;

import de.baumann.browser.R;
import de.baumann.browser.activitys.MainActivity;
import de.baumann.browser.iview.IBaseView;
import de.baumann.browser.utils.UserDataKt;
import de.baumann.browser.views.dialog.LoadingDialog;

/**
 * @author:
 * @date:2019/10/24 11:08
 * @discription: Activity 基类
 */
public abstract class BaseOdinActivity<V extends IBaseView, P extends BasePresenter<V>>
        extends AppCompatActivity implements IBaseView {

    protected Context mContext;
    private P presenter;
    private V view;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        windowSetting();
        setContentView(getLayout());
        mContext = this;
        initView();
        initmvp();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null && view != null) {
            presenter.unBind();
        }

        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    protected abstract int getLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract P createPresenter();

    protected abstract V createView();

    public P getPresenter() {
        return presenter;
    }

    protected V getView() {
        return view;
    }


    private void windowSetting() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    public void initmvp() {
        if (presenter == null) {
            presenter = createPresenter();
        }

        if (view == null) {
            view = createView();
        }

        if (presenter != null && view != null) {
            presenter.bind(view);
        }
    }

    protected void initToolbar(Toolbar toolbar, TextView title) {
        toolbar.setNavigationIcon(getBackIcon());
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(view -> back());
        if (title != null) {
            title.setText(getTitleText());
        }
    }

    public int getBackIcon() {
        return R.drawable.icon_home_back;
    }

    public String getTitleText() {
        return "";
    }

    protected void back() {
        finish();
    }

    LoadingDialog loadingDialog;

    @Override
    public void showLoading() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mContext);
        }
        loadingDialog.setLoadingText(getLoadingText());
        loadingDialog.show();
    }

    protected String getLoadingText() {
        return getString(R.string.loading);
    }

    @Override
    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onError(String errorMsg) {
        hideLoading();
        showShortToast(errorMsg);
    }

    @Override
    public void onError(int errorMsgId) {
        hideLoading();
        showShortToast(getString(errorMsgId));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9000 && resultCode == RESULT_OK) {
            initData();
            return;
        }
        if (requestCode == 9000 && resultCode == RESULT_CANCELED) {
            startActivity(new Intent(mContext, MainActivity.class));
        }
    }

    @Override
    public void tokenInvalid() {
        hideLoading();
//        if (UserDataKt.isLogined()) {
//        showShortToast(getString(R.string.token_expired));
        Log.d("lwq", "tokenInvalid: " + 1234567);
        UserDataKt.saveLogin(false);
//        UserDataKt.removeUser();
//        MainActivity.Companion.startMainActivity(this);

//        }
    }

    protected static final int BINDETH = 2300;

    @Override
    public void notEthUrl() {
//        BindEthDialog bindEthDialog = new BindEthDialog(mContext, v -> {
//            BindETHActivity.Companion.startBindETHActivity((Activity) mContext, BINDETH, "");
//        });
//        bindEthDialog.setOnCancelListener(dialog -> finish());
//        bindEthDialog.show();
    }

    public void showShortToast(String message) {
        runOnUiThread(() -> Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show());
    }

    protected void hideSoftInput(EditText view) {
        view.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public void showLoginDialog() {
//        CreateOrImportDialog createOrImportDialog = new CreateOrImportDialog(mContext);
//        createOrImportDialog.setSelectListener(new CreateOrImportDialog.SelectListener() {
//            @Override
//            public void selected(int type) {
//                switch (type) {
//                    case 0: {
//                        new OcWalletAgreementDialog(mContext).show();
//                        break;
//                    }
//                    case 1:
//                        ImportWalletActivity.Companion.startImportWalletActivity(mContext);
//                        startActivity(intent);
//
//                        break;
//                }
//            }
//    });
//        createOrImportDialog.show();

    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (UserDataKt.isLogined()) {
//            String ethAddress = UserDataKt.getUser().getEth_address();
//            if (ethAddress == null && showBindEth()) {
//                new BindEthDialog(mContext, view -> {
//                    BindETHActivity.Companion.startBindETHActivity(mContext, "");
//                }).show();
//            }
//        }
    }

    public boolean showBindEth() {
        return true;
    }

}
